package com.doudoukeji.service.Activity.mvp;

import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.doudoukeji.service.Activity.ConstantValue;
import com.doudoukeji.service.Activity.my.MyActivity;
import com.doudoukeji.service.Activity.my.edituser.editUserInfoActivity;
import com.doudoukeji.service.Activity.mypatient.MyPatientListActivity;
import com.doudoukeji.service.Activity.order.OrderListActivity;
import com.doudoukeji.service.R;

/**
 * @version 2.0
 * @类描述： 首页MVP
 * @项目名称：douDouTech
 * @类名称： SplashActivity
 * @包名称： com.doudoukeji.service.Activity.mvp
 * @创建人：张强 boy
 * @创建时间：2016/3/7 16:40
 * @修改人：
 * @修改时间：2016/3/7 16:40
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class MainActivity extends TabActivity implements View.OnClickListener {

    private TabHost tabHost;
    //取消状态下的图片
    private final int[] normal = new int[]{R.drawable.logo_order_nomal, R.drawable.logo_patient_nomal, R.drawable.logo_pressonal_nomal};
    //点击状态下的图片
    private final int[] press = new int[]{R.drawable.logo_order_press, R.drawable.logo_patient_press, R.drawable.logo_personal_press};
    private final String[] names = new String[]{"订单", "我的患者", "个人中心"};

    //底部图片和文本集合
    private ImageView[] images;
    private TextView[] textViews;

    //标题
    private TextView mTvTitle;
    private ImageView mImgEditor;

    //返回按钮
    private Button mBtnBack;
    private MyBroadcastReceiver mMyBroadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去除titilebar使用的
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);

        //初始化控件
        init();
    }

    /**
     * 初始化控件
     */
    private void init() {
        /***********标题栏************/
        initTopBar();

        /***********底边栏************/
        initBottomBar();

        /***********广播*****************/
        initBroadcast();

        try {
            this.tabHost = getTabHost();

            this.tabHost.addTab(this.tabHost.newTabSpec("tab_1").setContent(new Intent(this, OrderListActivity.class)).setIndicator("TAB1"));
            this.tabHost.addTab(this.tabHost.newTabSpec("tab_2").setContent(new Intent(this, MyPatientListActivity.class)).setIndicator("TAB2"));
            this.tabHost.addTab(this.tabHost.newTabSpec("tab_3").setContent(new Intent(this, MyActivity.class)).setIndicator("TAB3"));
            this.tabHost.setCurrentTab(0);
        } catch (Exception ex) {
            ex.printStackTrace();
            Log.e("EXCEPTION", ex.getMessage());
        }
    }

    /**
     * 初始化广播
     */
    private void initBroadcast() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConstantValue.Main_CHANGE);
        mMyBroadcastReceiver = new MyBroadcastReceiver();
        //消息红点
        registerReceiver(mMyBroadcastReceiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            unregisterReceiver(mMyBroadcastReceiver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化底边栏
     */
    private void initBottomBar() {
        findViewById(R.id.lay_main_pager).setOnClickListener(this);
        findViewById(R.id.lay_my_patient).setOnClickListener(this);
        findViewById(R.id.lay_personal_center).setOnClickListener(this);

        ImageView mIvMainPager = (ImageView) findViewById(R.id.iv_main_pager);
        ImageView mIvmy_patient = (ImageView) findViewById(R.id.iv_my_patient);
        ImageView mIvPersonalCenter = (ImageView) findViewById(R.id.iv_personal_center);
        images = new ImageView[]{mIvMainPager, mIvmy_patient, mIvPersonalCenter};

        TextView mTvMainPager = (TextView) findViewById(R.id.tv_main_pager);
        TextView mTvMyPatient = (TextView) findViewById(R.id.tv_my_patient);
        TextView MtvPersonalCenter = (TextView) findViewById(R.id.tv_personal_center);
        textViews = new TextView[]{mTvMainPager, mTvMyPatient, MtvPersonalCenter};
    }

    /**
     * 初始化标题栏
     */
    private void initTopBar() {

        // 设置标题
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvTitle.setText(names[0]);
        // 隐藏右侧按钮
        mBtnBack = (Button) findViewById(R.id.btn_back);
        mBtnBack.setVisibility(View.GONE);
        //設置編輯按鈕
        mImgEditor = (ImageView) findViewById(R.id.img_title);
        mImgEditor.setBackgroundResource(R.drawable.icon_editor);
        mImgEditor.setOnClickListener(this);

    }


    //设置状态选择效果
    private void seleted(int i) {
        for (int j = 0; j < images.length; j++) {
            if (j == i) {
                images[j].setBackgroundResource(press[j]);
                textViews[j].setTextColor(Color.parseColor("#feb333"));
                mTvTitle.setText(names[j]);
                this.tabHost.setCurrentTab(j);
            } else {
                images[j].setBackgroundResource(normal[j]);
                textViews[j].setTextColor(Color.parseColor("#777777"));
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.lay_main_pager:
                seleted(0);
                mImgEditor.setVisibility(View.GONE);
                break;
            case R.id.lay_my_patient:
                mImgEditor.setVisibility(View.GONE);
                seleted(1);
                break;
            case R.id.lay_personal_center:
                seleted(2);
                mImgEditor.setVisibility(View.VISIBLE);
                break;
            case R.id.img_title:
                Intent intent = new Intent(this,editUserInfoActivity.class);
                startActivity(intent);
                break;
        }
    }

    private class MyBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(ConstantValue.Main_CHANGE)) {
                String position = intent.getStringExtra("position");
                tabHost.setCurrentTab(Integer.parseInt(position));
                seleted(Integer.parseInt(position));
            }
        }
    }


}
